package com.kt.appinfo.apinfo.contorller;

import com.kt.appinfo.apinfo.pojo.DevUser;
import com.kt.appinfo.apinfo.servlet.DevServlet;
import com.kt.appinfo.apinfo.tools.Constants;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@Log4j2
@RequestMapping("/dev")
public class Contorller {
    @Autowired
    private DevServlet devServlet;


    @RequestMapping(value = "/login")
    public String toLogin(){
        System.out.println("进入后台");
        return "devlogin";
    }




    @RequestMapping(value = "/dologin", method = RequestMethod.POST)
    public String doLogin(@RequestParam("devCode") String devCode
            , @RequestParam("devPassword") String devpassword,
                          Model model,
                          HttpServletRequest request) {
        try {
            DevUser devUser = devServlet.dologin(devCode, devpassword);
            if (devUser == null) {
                model.addAttribute("error", "用户名密码错误");
                return "redirect:login";
            } else {
                request.getSession().setAttribute("devUser", "devCode");
                return "developer/main";
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            model.addAttribute("error", "后台接出现异常");
            return "redirect:../../403";
        }


    }

    @RequestMapping(value = "/flatfrom/main")
    public String main(HttpSession session) {
       if (session.getAttribute(Constants.USER_SESSION)==null){
           return "redirect:/dev/login";
       }
       return "redirect:/dev/main";
    }
    @RequestMapping(value = "/logout")
    public  String logout(HttpSession session){
        session.removeAttribute(Constants.USER_SESSION);
        return "devlogin";
    }
}

